package com.aiti.lulian.controller;

import cn.hutool.core.util.StrUtil;
import com.aiti.base.core.base.controller.WebController;
import com.aiti.base.core.utils.message.Message;
import com.aiti.lulian.dto.CardEnterpriseTemplateDto;
import com.aiti.lulian.entity.CardEnterpriseTemplate;
import com.aiti.lulian.entity.LuLianEnterpriseExtension;
import com.aiti.lulian.entity.LuLianEnterpriseUser;
import com.aiti.lulian.feign.CloudShowManagerSubService;
import com.aiti.lulian.service.CardEnterpriseTemplateService;
import com.aiti.lulian.service.ILuLianEnterpriseExtensionService;
import com.aiti.lulian.service.ILuLianEnterpriseUserService;
import com.aiti.lulian.vo.CardEnterpriseTemplateBatchVo;
import com.aiti.lulian.vo.CardEnterpriseTemplateVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Api(tags = "企业名片模板控制层")
@RestController
@RequestMapping("enterprise/template")
public class CardEnterpriseTemplateController extends WebController {
    @Autowired
    private CardEnterpriseTemplateService cardEnterpriseTemplateService;
    @Autowired
    private ILuLianEnterpriseUserService luLianEnterpriseUserService;
    @Autowired
    private ILuLianEnterpriseExtensionService extensionService;




    @PostMapping("/save")
    public Message save(@RequestBody CardEnterpriseTemplate enterpriseTemplate){
        return cardEnterpriseTemplateService.saveTemplate(enterpriseTemplate);
    }

    @GetMapping("/detail")
    public Message detail(@RequestParam(value = "enterpriseId",required = false) String enterpriseId){
        if(StringUtils.isBlank(enterpriseId)){
            String userId = findUserId();
            LuLianEnterpriseUser enterpriseUser = luLianEnterpriseUserService.getOne(new QueryWrapper<LuLianEnterpriseUser>().eq("user_id", userId));
            if(enterpriseUser != null){
                return cardEnterpriseTemplateService.getByEnterpriseId(enterpriseUser.getEnterpriseId());
            }else{
                return Message.success("该员工不是企业用户");
            }

        }else{
            return cardEnterpriseTemplateService.getByEnterpriseId(enterpriseId);
        }

    }

    @ApiOperation("查询名片模板列表")
    @GetMapping("/list")
    public Message queryList(CardEnterpriseTemplateVo cardEnterpriseTemplate){
        return cardEnterpriseTemplateService.selectList(cardEnterpriseTemplate);
    }

    @DeleteMapping("/delete")
    public Message delete(@RequestParam("baseId") String baseId){
        return cardEnterpriseTemplateService.deleteTemplate(baseId);
    }

    @GetMapping
    public Message queryDetailsByBaseId(@RequestParam("baseId") String baseId){
        return Success(cardEnterpriseTemplateService.getById(baseId));
    }

    @PostMapping("/saveBatch")
    public Message saveBatch(@Validated @RequestBody CardEnterpriseTemplateBatchVo vo){
        vo.setUserId(findUserId());
        return cardEnterpriseTemplateService.saveTemplateBatch(vo);
    }

    @GetMapping("/queryEnterpriseBgByEnterpriseId")
    public Message queryEnterpriseBgByEnterpriseId(@RequestParam("enterpriseId") String enterpriseId){
        if(StringUtils.isBlank(enterpriseId)){
            return Message.success();
        }
        LuLianEnterpriseExtension enterpriseExtension = extensionService.getOne(new QueryWrapper<LuLianEnterpriseExtension>().eq("enterprise_id", enterpriseId));
        if(enterpriseExtension == null){
            return Message.success();
        }
        String layoutNum = enterpriseExtension.getLayoutNum();
        if(StringUtils.isBlank(layoutNum)){
            return Message.success();
        }
        List<CardEnterpriseTemplate> list = cardEnterpriseTemplateService.list(new QueryWrapper<CardEnterpriseTemplate>().eq("layout_num", layoutNum).eq("enterprise_id", enterpriseId));
        if(list == null || list.isEmpty()){
            return Message.success();
        }
        CardEnterpriseTemplate cardEnterpriseTemplate = list.get(0);
        return Message.success(cardEnterpriseTemplate.getBaseId());
    }

//    @GetMapping("/templateDetail")
//    public Message templateDetail(@RequestParam(value = "baseId") String baseId, @RequestParam(value = "userId", required = false) String userId){
//        String currentUserId = StringUtils.isEmpty(userId) ? findUserId() : userId;
//        LuLianEnterpriseUser enterpriseUser = luLianEnterpriseUserService.getOne(new QueryWrapper<LuLianEnterpriseUser>().eq("user_id", currentUserId));
//        if(enterpriseUser != null){
//            return cardEnterpriseTemplateService.templateDetail(baseId, enterpriseUser.getEnterpriseId(), currentUserId, StringUtils.isEmpty(userId) ? false : true);
//        }else{
//            return Message.success("该员工不是企业用户");
//        }
//
//    }

    @GetMapping("/templateStyleDetail")
    public Message templateStyleDetail(@RequestParam(value = "styleId") String styleId, @RequestParam(value = "templateId", required = false) String templateId, @RequestParam(value = "cardId", required = false) String cardId, @RequestParam(value = "templateType") String templateType){
        return cardEnterpriseTemplateService.templateStyleDetail(styleId, templateId, cardId, templateType);

    }


    @PostMapping("/saveTemplate")
    public Message saveTemplate(@RequestBody @Validated CardEnterpriseTemplateDto dto){
        return cardEnterpriseTemplateService.saveTemplate(dto);
    }

    @PutMapping("/updateTemplate")
    public Message updateTemplate(@RequestBody @Validated CardEnterpriseTemplateDto dto){
        return cardEnterpriseTemplateService.updateTemplate(dto);
    }

//    @GetMapping("/enterpriseTemplateList")
//    public Message enterpriseTemplateList(@RequestParam(value = "userId", required = false) String userId){
//        String currentUserId = StringUtils.isEmpty(userId) ? findUserId() : userId;
//        LuLianEnterpriseUser enterpriseUser = luLianEnterpriseUserService.getOne(new QueryWrapper<LuLianEnterpriseUser>().eq("user_id", currentUserId));
//        if(enterpriseUser != null){
//            return cardEnterpriseTemplateService.enterpriseTemplateList(enterpriseUser.getEnterpriseId());
//        }else{
//            return Message.success("该员工不是企业用户");
//        }
//
//    }
//
//    @GetMapping("/userTemplateList")
//    public Message userTemplateList(@RequestParam(value = "userId", required = false) String userId){
//        String currentUserId = StringUtils.isEmpty(userId) ? findUserId() : userId;
//        LuLianEnterpriseUser enterpriseUser = luLianEnterpriseUserService.getOne(new QueryWrapper<LuLianEnterpriseUser>().eq("user_id", currentUserId));
//        if(enterpriseUser != null){
//            return cardEnterpriseTemplateService.userTemplateList(enterpriseUser.getEnterpriseId(), currentUserId);
//        }else{
//            return Message.success("该员工不是企业用户");
//        }
//
//    }


    @GetMapping("/preTemplateStyleDetail")
    public Message preTemplateStyleDetail(@RequestParam(value = "styleId") String styleId, @RequestParam(value = "templateId", required = false) String templateId, @RequestParam(value = "templateType") String templateType, @RequestParam(value = "version", required = false) String version){
        return cardEnterpriseTemplateService.preTemplateStyleDetail(styleId, templateId, templateType,version);

    }


    @PostMapping("/savePreTemplate")
    public Message savePreTemplate(@RequestBody @Validated CardEnterpriseTemplateDto dto){
        return cardEnterpriseTemplateService.savePreTemplate(dto);
    }

    @PutMapping("/updatePreTemplate")
    public Message updatePreTemplate(@RequestBody @Validated CardEnterpriseTemplateDto dto){
        return cardEnterpriseTemplateService.updatePreTemplate(dto);
    }
}
